package weeny.kdtree;


import java.awt.geom.Point2D;

public class KdTreeUtil extends AbstractKdTreeUtil{
	/**
	 * Very simple concrete implementation. By default, we only consider three
	 * types of objects: double[], float[], and Point2D, and throw an exception 
	 * for anything else.
	 */
	protected double[] toArray(Object obj){
		if (obj instanceof double[]) {
			return ((double[]) obj).clone();
		}
		else if (obj instanceof float[]) {
			double[] result = new double[((float[]) obj).length];
			float[] floatArray = (float[]) obj;
			for (int i = 0; i<floatArray.length; i++){
				result[i] = floatArray[i];
			}
			return result;
		}
		else if (obj instanceof Point2D) {
			Point2D pnt = (Point2D) obj;
			double[] result = new double[]{pnt.getX(),pnt.getY()};
			return result;
		}
		else {
			throw new IllegalArgumentException("cannot handle "+obj.getClass());
		}
	}
}